// Слой для шаблонов
package dialogs
{
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import components.pj_tabbox;
	
	import editor.*;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class pi_cards extends pj_tabbox {
		public function pi_cards(parent:uif_component, list:Array, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, list, ax, ay, awidth, aheight, awidth - 150, styleClass);
			var inner: Rectangle = getInner();
			(new uif_label(this, inner.x + inner.width - 100, inner.y, 100, 30, 'box_label')).text = pi_data.TEMPLATES;
			SetCurrentCard(pj.def.DEFAULT_MASK, pj.def.DEFAULT_GROUP);
			app.addEventListener(pi_events.SETCURRENTCARD, doSetCurrentCard);
		}
		
		protected function doSetCurrentCard(e: *): void {
			var card_id: int = parseInt(e.object as String);
			SetCurrentCard(card_id, pi_data.infoFromID(card_id).group);
		}
		
		protected function SetCurrentCard(card_id: int, group_id: int): void {
			function selectCard(group_index: int, card_index: int): void {
				tabs.selectIndex 		= group_index;
				_itemsLayer.selectIndex = card_index;
				app.dispatchEvent(new ObjectEvent(Event.SELECT, _itemsLayer.items[card_index].control));
			}
			if (group_id > -1) { // Если группа задана 
				pi_data.cardGroup.forEach(function(item:*, index:int, array:Array): void {
					if (item.group == group_id) {
						tabs.selectIndex = index;
						for (var i:int; i<_itemsLayer.items.length; i++) {
							if (_itemsLayer.items[i].object.id == card_id) {
								selectCard(index, i);
								return;
							}
						}
					}
				});
				if ((tabs.selectIndex == -1) || (_itemsLayer.selectIndex == -1)) selectCard(0, 0);
			} else selectCard(0, 0);
		}
		
		public function assignCards(filterList: Array): void {
			filterList.sortOn('order', Array.NUMERIC | Array.DESCENDING);
			var a_rows: int  = 2;
			var item_width: int = _itemsLayer.calcFrameWidth(a_rows);
			
			while (filterList.length < _itemsLayer.countDisplay) filterList.push(null);
			
			_itemsLayer.assignList(filterList, item_width, function(_scroll: uif_component, inner: Rectangle, index: int): pi_cardPreview {
					return new pi_cardPreview(index, _scroll, inner.x, inner.y, inner.width, inner.height, filterList[index], 'tabItem_unselect');
				}
			, a_rows);
			_itemsLayer.scrollStep = _itemsLayer.countDisplay / 2;
		}
		
		override protected function doChangeSelect(tabIndex: int): void {
			super.doChangeSelect(tabIndex);
			var groupObj	: Object 	= pi_data.cardGroup[tabIndex];
			if (groupObj.hasOwnProperty('url')) {
				(app as photo_inject).loadGroupMI(groupObj, function(groupObj: Object): void {
					doChangeSelect(tabIndex);
				});
			} else {
				var filterList	: Array 	= new Array();
				for each (var item: Object in pi_data.maskInfoList) {
					if (item)
						if (((groupObj.group != undefined) && (item.group == groupObj.group)) || 
							((groupObj.filter != undefined) && (groupObj.filter(item)))) {
							if (item.published == false) continue;
		//					var order: Number = item['order']?item.order:0;
							if (!item.order) {
								var nid: Number = ((item.id>=4775) && (item.id < 5000))?(item.id + 450):item.id;
								item.order = item.price?(item.price * 20000):nid;
							}
							filterList.push(item);
						}	
				}
				
				assignCards(filterList);
			}
		}
		
	}
}